package com.yimixian.app.ui;

import com.yimixian.app.R;
import com.yimixian.app.adapter.HomeListViewAdapter;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by zhilian-2 on 2015/10/15.
 */
public class HomeListItemChildView extends ViewGroup {
    private static final int PADDING_HOR = 0;//水平方向padding
    private static final int PADDING_VERTICAL = 5;//垂直方向padding
    private static final int SIDE_MARGIN = 0;//左右间距
    private static final int TEXT_MARGIN = 0;
    private static final int MARGIN_LEFT_RIGHT = 20;
    int screenWidth = 720;
    private static final int columnCount = 2;//列数
    private HomeListViewAdapter.onItemViewClickListener mListener;
    /**
     * @param context
     */
    public HomeListItemChildView(Context context) {
        super(context);
    }
    /**
     * @param context
     * @param attrs
     */
    public HomeListItemChildView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public HomeListItemChildView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void bind(final int position, List<String> mList, int width,
                     final HomeListViewAdapter.onItemViewClickListener mListener){
        removeAllViews();
        this.mListener = mListener;
        this.screenWidth = width - 3 * MARGIN_LEFT_RIGHT;
        for (int i = 0; i < mList.size(); i++){
            View item = LayoutInflater.from(getContext()).inflate(
                    R.layout.home_list_item_child, null);
            item.setBackgroundResource(R.drawable.settings_item_bg);

            ImageView imageView = (ImageView) item.findViewById(R.id.image_view);
            LinearLayout.LayoutParams paramsImage = new LinearLayout.LayoutParams
                    (screenWidth/columnCount, screenWidth/columnCount);
            imageView.setLayoutParams(paramsImage);

            TextView sortTitle = (TextView) item.findViewById(R.id.title);
            sortTitle.setText(mList.get(i));

//            ImageButton btn = (ImageButton) item.findViewById(R.id.add_btn);
//            btn.setTag(i);
//            btn.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (mListener != null)
//                        mListener.onItemViewClick(view, position, (int) view.getTag(), getPoint(view));
//                }
//            });

            item.setTag(i);
            item.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null)
                        mListener.onItemViewClick(view, position, (int) view.getTag(), null);
                }
            });
            this.addView(item);
        }

    }

    public Point getPoint(View view) {
        int[] location = new int[2];
        view.getLocationInWindow(location);
        Point point = new Point(location[0], location[1]);
        return point;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int autualWidth = r - l;
        int x = SIDE_MARGIN;// 横坐标开始
        int y = 0;//纵坐标开始
        int rows = 1;
        for(int i=0;i<childCount;i++){
            View view = getChildAt(i);
            int width = screenWidth/columnCount;
            int height = view.getMeasuredHeight();
//            x += width+TEXT_MARGIN;
            x += width+MARGIN_LEFT_RIGHT;
            if(x>autualWidth){
//                x = width+SIDE_MARGIN;
                x = width + MARGIN_LEFT_RIGHT;
                rows++;
            }
            y = rows*(height+TEXT_MARGIN);
            if(i==0){
                view.layout(x-width-TEXT_MARGIN, y-height, x-TEXT_MARGIN, y);
            }else{
                view.layout(x-width, y-height, x, y);
            }
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int x = 0;//横坐标
        int y = 0;//纵坐标
        int rows = 1;//总行数
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
//        int actualWidth = specWidth - SIDE_MARGIN * 2;//实际宽度
        int actualWidth = specWidth - MARGIN_LEFT_RIGHT * 3;
        int childCount = getChildCount();
        for(int index = 0;index<childCount;index++){
            View child = getChildAt(index);
//            child.setPadding(PADDING_HOR, PADDING_VERTICAL, PADDING_HOR, PADDING_VERTICAL);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = screenWidth/columnCount;
            int height = child.getMeasuredHeight();
            x += width+TEXT_MARGIN;
            if(x>actualWidth){//换行
                x = width;
                rows++;
            }
            y = rows*(height+TEXT_MARGIN);
        }
        setMeasuredDimension(specWidth, y);
    }

}